package com.example.petcare.controller;


import com.example.petcare.pojo.Result;
import com.example.petcare.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController// 标记为控制器类,并且返回json数据
@RequestMapping
public class FileUploadController {

    @PostMapping("/user/pet/upload")
    public Result<String> uploadUser(MultipartFile file) throws Exception {


        // 获取文件的原始名字
        String originalFilename = file.getOriginalFilename();

        // 保证文件的名字是唯一的，从而防止文覆盖
        String fileName = UUID.randomUUID().toString()+"_"+originalFilename.substring(originalFilename.lastIndexOf("."));
        // 下划线 _：作为分隔符，增强可读性。

//        file.transferTo(new File("F:\\files\\"+fileName));

        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());// 调用AliOssUtil的uploadFile方法,将文件上传到阿里云OSS中


        return Result.success(url);

    }

    @PostMapping("/provider/upload")
    public Result<String> uploadProvider(MultipartFile file) throws Exception {


        // 获取文件的原始名字
        String originalFilename = file.getOriginalFilename();

        // 保证文件的名字是唯一的，从而防止文覆盖
        String fileName = UUID.randomUUID().toString()+"_"+originalFilename.substring(originalFilename.lastIndexOf("."));
        // 下划线 _：作为分隔符，增强可读性。

//        file.transferTo(new File("F:\\files\\"+fileName));

        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());


        return Result.success(url);

    }



}
